package cn.edu.buaa.cnsatm.traffic_big_data_server.highway.model;

import cn.edu.buaa.cnsatm.traffic_big_data_server.highway.dao.ImageDAO;
import cn.edu.buaa.cnsatm.traffic_big_data_server.util.DateUtil;
import cn.edu.buaa.cnsatm.traffic_big_data_server.util.JDBCUtil;
import cn.edu.buaa.cnsatm.traffic_big_data_server.util.PropertiesUtil;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ImageService {
    /**
     * @author: LX
     * @className: ImageService
     * @description: 云图、路天气图等图片的相关服务
     * @data: 2019-10-29
     **/
    public static String getNewestImageByImageType(String imageType) throws SQLException {
        /**
         * @author: LX
         * @className: getNewestImage
         * @description: 根据请求类型给出数据库中最新的云图、路天气图
         * @data: 2019-10-29
         **/
        //String currentTime = DateUtil.getCurrentTime();
        ResultSet resultSet = ImageDAO.getLatestImage(imageType);
        resultSet.next();
        JsonObject imageJson = new JsonObject();
        String mappingDir = null;
        if(imageType == "cloud"){mappingDir = "CLOUD_IMAGE_MAPPING_DIRECTORY";}
        if(imageType == "road"){mappingDir = "ROAD_IMAGE_MAPPING_DIRECTORY";}
        mappingDir = PropertiesUtil.properties.getProperty(mappingDir);//获取配置文件里的映射地址
        mappingDir = mappingDir.substring(0, mappingDir.length() - 2);//去掉末尾的两个*

        String imageURL = mappingDir + resultSet.getString("image_name");//拼接为映射后的图片url，不带ip和port


        imageJson.addProperty("image_url", imageURL);
        imageJson.addProperty("update_time", resultSet.getString("update_time"));
        imageJson.addProperty("image_time", resultSet.getString(3));
        resultSet.getStatement().close();
        resultSet.close();
        return imageJson.toString();
    }
    public static void main(String args[]) throws IOException, SQLException {
        JDBCUtil.startConnection();
        System.out.println(getNewestImageByImageType("road"));

    }
}
